package com.example.controller;

import com.example.common.enums.ResponseCodeEnum;
import com.example.common.utils.ApiResult;
import com.example.model.entity.PocEvent;
import com.example.model.query.PersonQuery;
import com.example.model.query.PocEventQuery;
import com.example.model.vo.FileVo;
import com.example.model.vo.PocEventVo;
import com.example.model.vo.SuppliesVo;
import com.example.service.PersonService;
import com.example.service.PocEventService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * /pocEvent Controller
 * 2020-08-27 18:59
 */
@Controller
@RestController
@RequestMapping("/pocEvent")
@CrossOrigin
@Api(value = "事件管理",tags={"事件管理接口"})
public class PocEventController {

    @Autowired
    private PocEventService pocEventService;
    /*
     * 上传文件
     */
    @ApiOperation(value = "上传文件")
    @RequestMapping(value = "/upload",method=RequestMethod.POST)
    @ResponseBody
    public ApiResult<FileVo> upload(HttpServletRequest request, HttpServletResponse response, MultipartFile file)  {
        try {
            return pocEventService.imgUpload(request, response, file);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_SAVE_CHANNEL_NAME_FALSE);
        }
    }

    /*
    预览文件
    */
    @ApiOperation(value = "预览文件")
    @RequestMapping(value = "/preview",method=RequestMethod.POST)
    @ResponseBody
    public ApiResult<String> preview(@RequestBody String path) throws Throwable {
            return pocEventService.preview(path);
    }
    /**
     * 分页查询列表
     */
    @ApiOperation(value = "分页查询事件列表")
    @GetMapping("/page")
    public ApiResult<PocEventQuery> pageByQuery(PocEventQuery query) {
        try {
            return pocEventService.pageUserVoByQuery(query);
        }   catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_PAGE_AD_FALSE);
        }
    }

    @ApiOperation(value = "新增事件")
    @PostMapping("/save")
    public ApiResult<PocEvent> save(@RequestBody PocEventVo pocEventVo) {
        try {
            return pocEventService.save(pocEventVo);
        }  catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<>( ResponseCodeEnum.MALL_SERVICE_SAVE_AD_FALSE);
        }
    }

    @ApiOperation(value = "编辑事件")
    @PostMapping("/modify")
    public ApiResult<Boolean> update(@RequestBody PocEventVo pocEventVo) {
        try {
            return pocEventService.update(pocEventVo);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_ORDER_UPDATE_FALSE);
        }
    }

    @ApiOperation(value = "事件详情")
    @GetMapping("/get/{id}")
    public ApiResult<PocEventVo> get(@PathVariable("id") String id){
        try {
            return pocEventService.getInfo(id);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_ORDER_GET_FALSE);
        }
    }

    @ApiOperation(value = "删除事件")
    @GetMapping("/del/{id}")
    public ApiResult<Boolean> del(@PathVariable("id") String id){
        try {
            return pocEventService.del(id);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_ORDER_GET_FALSE);
        }
    }

    @ApiOperation(value = "修改事件状态")
    @GetMapping("/modifyStatus/{id}/{status}")
    public ApiResult<Boolean> modifyStatus(@PathVariable("id") String id,@PathVariable("status") Long status){
        try {
            return pocEventService.modifyStatus(id,status);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_ORDER_GET_FALSE);
        }
    }
    /*
    * 导出
    * */
    @ApiOperation(value = "事件excel导出")
    @GetMapping("/export")
    public ApiResult<Boolean> export(PocEventQuery query, HttpServletRequest request, HttpServletResponse response) {
        try{
            return pocEventService.export(query, request, response);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.FDMALL_CUSTOMER_SERVICE_EXPORTECUSTOMER_FALSE);
        }
    }
}
